﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC
{
    [Serializable]
#if NO_SILVERLIGHT
    [Browsable(true)]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [EditorBrowsable(EditorBrowsableState.Always)]
#endif
    [DataContract]
    public class IdentifierAndValue : INotifyPropertyChanged
    {
        [DataMember]
        public string _identifier;

        [DataMember]
        public object _value;

        public string Identifier
        {
            get { return _identifier; }
            set
            {
                if (!_identifier.AreEqual(value))
                {
                    _identifier = value;

                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("Identifier"));
                }
            }
        }

        public object Value
        {
            get { return _value; }
            set
            {
                if (!_value.AreEqual(value))
                {
                    _value = value;

                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("Value"));
                }
            }
        }

        public IdentifierAndValue() { }

        public IdentifierAndValue(string identifier, object value)
        {
            Identifier = identifier;
            Value = value;
        }

        #region "Implicit Operator Conversions"

        public static implicit operator int(IdentifierAndValue value)
        {
            if (value.Value != null)
            {
                try { return Convert.ToInt32(value.Value); }
                catch { }
            }
            return int.MinValue;
        }

        public static implicit operator double(IdentifierAndValue value)
        {
            if (value.Value != null)
            {
                try { return Convert.ToDouble(value.Value); }
                catch { }
            }
            return double.MinValue;
        }

        public static implicit operator string(IdentifierAndValue value)
        {
            if (value.Value != null)
            {
                try { return Convert.ToString(value.Value); }
                catch { }
            }
            return string.Empty;
        }

        public static implicit operator DateTime(IdentifierAndValue value)
        {
            if (value.Value != null)
            {
                try { return Convert.ToDateTime(value.Value); }
                catch { }
            }
            return DateTime.MinValue;
        }

        #endregion

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;
    }
}